package com.mtons.mblog.im.controller;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.map.MapUtil;
import cn.hutool.json.JSONUtil;
import com.mtons.mblog.im.common.Constant;
import com.mtons.mblog.im.common.Result;
import com.mtons.mblog.im.service.ChatService;
import com.mtons.mblog.im.vo.ImUser;
import com.mtons.mblog.modules.data.AccountProfile;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.*;

import com.mtons.mblog.web.controller.BaseController;

@Controller
@Slf4j
public class InitDataController extends BaseController {

    @Autowired
    ChatService chatService;

    @GetMapping("/im")
    public String chat(@RequestParam(value = "userId", defaultValue = "1") long userId, HttpSession session) {
       // session.setAttribute("userId", userId);
        return "/im/index";
    }

    /**
     * 获得群组的成员信息
     *
     * @param session
     * @param request
     * @return
     */
    @ResponseBody
    @GetMapping("/getMineAndGroupData")
    public Result getMineAndGroupData(HttpSession session, HttpServletRequest request) {
        // 获取登录的用户信息
        AccountProfile up = getProfile();
        //long pid = ServletRequestUtils.getLongParameter(request, "pid", 0);

        long pid = up.getId();
        /*if (!isAuthenticated()) {
            //  return Result.fail("请先登录");
        }*/

        String name = up.getName();
        Long userId = (Long) session.getAttribute(up.getId() + "");

        //Long userId = (Long) session.getAttribute("userId");

        ImUser user = new ImUser();
        //user.setAvatar("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1546945020871&di=180b59ba117ef145d4388fc8bf8e63d4&imgtype=0&src=http%3A%2F%2Fpic136.nipic.com%2Ffile%2F20170725%2F10673188_152520789000_2.jpg");
        user.setAvatar(up.getAvatar());
        user.setId(userId);
        user.setUsername(up.getName());
        user.setStatus(ImUser.ONLINE_STATUS);

        // 默认群
        Map<String, Object> group = new HashMap<>();
        group.put("name", "社区聊天");
        group.put("type", "group");
        group.put("avatar", "https://dwz.cn/c84QeOKg");
        group.put("id", Constant.DEFAULT_GROUP_ID);
        group.put("members", 0);

        return Result.ok(MapUtil.builder()
                .put("mine", user)
                .put("group", group)
                .map());

    }

    /**
     * 从通道中获取所有用户信息
     *
     * @return
     */
    @ResponseBody
    @GetMapping("/getMembers")
    public Result getMembers() {
        Set<Object> members = chatService.findAllOnlineMembers();

        log.info("获取群成员---------->" + JSONUtil.toJsonStr(members));

        return Result.ok(MapUtil.of("list", members));
    }

    @ResponseBody
    @GetMapping("/getGroupHistoryMsg")
    public Result getGroupHistoryMsg() {

        List<Object> messages = chatService.getGroupHistoryMsg(20);
        return Result.ok(messages);
    }

}
